/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.newgui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import sifisy.core.Model;

/**
 *
 * @author lzanini
 */
public class NewProjectWidget extends JDialog {
    private MainWidget parent;
    private JTextField tfProjectName;
    private JButton btConfirm;
    private JButton btCancel;
    private boolean validName;

    private void initComponents() {
        tfProjectName = new JTextField(15);
        btConfirm = new JButton("Ok");
        btCancel = new JButton(Model.translate("cancel"));

        btConfirm.setEnabled(false);

        // Adding listeners
        tfProjectName.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                validName = true;
                if (tfProjectName.getText().isEmpty() ||
                        MainWidget.getProjectsList().contains(tfProjectName.getText())) {
                    validName = false;
                }
                btConfirm.setEnabled(validName);
            }
        });
        btConfirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (validName == true) {
                    MainWidget.createProject(tfProjectName.getText());
                    dispose();
                }
            }
        });
        btCancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
        });
    }

    public NewProjectWidget(MainWidget parent) {
        super((JFrame)parent, Model.translate("newProject"), true);
        this.parent = parent;
        initComponents();
        setLayout(new BorderLayout());

        JPanel nameLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        nameLabel.add(new JLabel(Model.translate("name") + ":"));

        JPanel nameText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        nameText.add(tfProjectName);

        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        namePanel.setBorder(new TitledBorder(Model.translate("addProject")));
        namePanel.add(nameLabel);
        namePanel.add(nameText);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttons.add(btCancel);
        buttons.add(btConfirm);

        add(namePanel,BorderLayout.NORTH);
        add(buttons,BorderLayout.CENTER);
        setSize(280,140);
        setLocationRelativeTo(parent);
        setVisible(true);
    }
}
